/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include <shell/shared/imageWriter/stb/ImageWriterSTB.h>

#include <limits>
#include <string>
#include <shell/shared/imageLoader/ImageLoader.h>
#include <igl/IGL.h>
#if defined(IGL_CMAKE_BUILD)
#define STB_IMAGE_WRITE_IMPLEMENTATION
#endif // IGL_CMAKE_BUILD
#include <stb_image_write.h>

namespace igl::shell {

void ImageWriterSTB::writeImage(const std::string& imageAbsolutePath,
                                const ImageData& imageData,
                                bool flipY) const noexcept {
  if (imageData.desc.width * 4 > std::numeric_limits<int>::max() ||
      imageData.desc.height > std::numeric_limits<int>::max()) {
    IGLLog(IGLLogError, "Failed saving the file: %s", imageAbsolutePath.c_str());
    return;
  }
  stbi_flip_vertically_on_write(static_cast<int>(flipY));

  IGL_DEBUG_ASSERT(imageAbsolutePath.length() > 4);
  auto ext = imageAbsolutePath.substr(imageAbsolutePath.length() - 4);
  std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  int ret = 0;
  if (ext == ".png") {
    ret = stbi_write_png(imageAbsolutePath.c_str(),
                         static_cast<int>(imageData.desc.width),
                         static_cast<int>(imageData.desc.height),
                         4,
                         imageData.data->data(),
                         /*int stride_in_bytes*/ 4 * static_cast<int>(imageData.desc.width));
  } else if (ext == ".bmp") {
    ret = stbi_write_bmp(imageAbsolutePath.c_str(),
                         static_cast<int>(imageData.desc.width),
                         static_cast<int>(imageData.desc.height),
                         4,
                         imageData.data->data());
  }
  if (!ret) {
    IGLLog(IGLLogError, "Failed saving the file: %s", imageAbsolutePath.c_str());
  }
}

} // namespace igl::shell
